package com.inei.cenacom.model.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

import com.inei.cenacom.model.entity.Cap200;
import com.inei.cenacom.model.entity.Cap300;

public class Cap300Dao extends ACRUD<Cap300> {

	public Cap300Dao(Context context) {
		super(context);
	}

	@Override
	public List<Cap300> leerTodo() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Cap300> leer(String id, int anio, int NRO_MREG) {
		List<Cap300> lista = null;
		db = util.getReadableDatabase();
		c = db.rawQuery(
				"select * from CAP300 where ID_N=?and ANIO=? and NRO_MREG=?",
				new String[] { id, anio + "", NRO_MREG + "" });
		if (c.moveToFirst()) {
			lista = new ArrayList<Cap300>();
			do {
				Cap300 o = new Cap300();
				o.setID_N(id);
				o.setANIO(anio);
				o.setNRO_MREG(NRO_MREG);
				o.setNRO_VFREG(c.getInt(c.getColumnIndex("NRO_VFREG")));
				o.setIVH301A(c.getString(c.getColumnIndex("IVH301A")));
				o.setIVH301B(c.getString(c.getColumnIndex("IVH301B")));
				o.setIVH301C(c.getString(c.getColumnIndex("IVH301C")));
				o.setIVH302(c.getInt(c.getColumnIndex("IVH302")));
				o.setIVH302_N(c.getString(c.getColumnIndex("IVH302_N")));
				o.setIVH303(c.getInt(c.getColumnIndex("IVH303")));
				o.setIVH304_DIA(c.getString(c.getColumnIndex("IVH304_DIA")));
				o.setIVH304_MES(c.getString(c.getColumnIndex("IVH304_MES")));
				o.setIVH304_ANIO(c.getString(c.getColumnIndex("IVH304_ANIO")));
				o.setIVH305(c.getInt(c.getColumnIndex("IVH305")));
				o.setIVH306(c.getInt(c.getColumnIndex("IVH306")));
				o.setIVH306_O(c.getString(c.getColumnIndex("IVH306_O")));
				o.setIVH307(c.getInt(c.getColumnIndex("IVH307")));
				o.setIVH307_O(c.getString(c.getColumnIndex("IVH307_O")));
				o.setIVH308(c.getInt(c.getColumnIndex("IVH308")));
				o.setIVH309(c.getInt(c.getColumnIndex("IVH309")));
				o.setIVH309_O(c.getString(c.getColumnIndex("IVH309_O")));
				o.setOBS_3(c.getString(c.getColumnIndex("OBS_3")));
				o.setFEC_INI(c.getString(c.getColumnIndex("FEC_INI")));
				o.setHOR_INI(c.getString(c.getColumnIndex("HOR_INI")));
				o.setFEC_FIN(c.getString(c.getColumnIndex("FEC_FIN")));
				o.setHOR_FIN(c.getString(c.getColumnIndex("HOR_FIN")));
				o.setUSR_CREACION(c.getString(c.getColumnIndex("USR_CREACION")));
				o.setFEC_CREACION(c.getString(c.getColumnIndex("FEC_CREACION")));
				o.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
				o.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
				lista.add(o);
			} while (c.moveToNext());
		} else {
			lista = null;
		}
		cerrar();
		return lista;
	}

	@Override
	public List<Cap300> leer(String id, int anio) {
		List<Cap300> lista = null;
		db = util.getReadableDatabase();
		c = db.rawQuery("select * from CAP300 where ID_N=? and ANIO=?",
				new String[] { id, anio + "" });
		if (c.moveToFirst()) {
			lista = new ArrayList<Cap300>();
			do {
				Cap300 o = new Cap300();
				o.setID_N(id);
				o.setANIO(anio);
				o.setNRO_MREG(c.getInt(c.getColumnIndex("NRO_MREG")));
				o.setNRO_VFREG(c.getInt(c.getColumnIndex("NRO_VFREG")));
				o.setIVH301A(c.getString(c.getColumnIndex("IVH301A")));
				o.setIVH301B(c.getString(c.getColumnIndex("IVH301B")));
				o.setIVH301C(c.getString(c.getColumnIndex("IVH301C")));
				o.setIVH302(c.getInt(c.getColumnIndex("IVH302")));
				o.setIVH302_N(c.getString(c.getColumnIndex("IVH302_N")));
				o.setIVH303(c.getInt(c.getColumnIndex("IVH303")));
				o.setIVH304_DIA(c.getString(c.getColumnIndex("IVH304_DIA")));
				o.setIVH304_MES(c.getString(c.getColumnIndex("IVH304_MES")));
				o.setIVH304_ANIO(c.getString(c.getColumnIndex("IVH304_ANIO")));
				o.setIVH305(c.getInt(c.getColumnIndex("IVH305")));
				o.setIVH306(c.getInt(c.getColumnIndex("IVH306")));
				o.setIVH306_O(c.getString(c.getColumnIndex("IVH306_O")));
				o.setIVH307(c.getInt(c.getColumnIndex("IVH307")));
				o.setIVH307_O(c.getString(c.getColumnIndex("IVH307_O")));
				o.setIVH308(c.getInt(c.getColumnIndex("IVH308")));
				o.setIVH309(c.getInt(c.getColumnIndex("IVH309")));
				o.setIVH309_O(c.getString(c.getColumnIndex("IVH309_O")));
				o.setOBS_3(c.getString(c.getColumnIndex("OBS_3")));
				o.setFEC_INI(c.getString(c.getColumnIndex("FEC_INI")));
				o.setHOR_INI(c.getString(c.getColumnIndex("HOR_INI")));
				o.setFEC_FIN(c.getString(c.getColumnIndex("FEC_FIN")));
				o.setHOR_FIN(c.getString(c.getColumnIndex("HOR_FIN")));
				o.setUSR_CREACION(c.getString(c.getColumnIndex("USR_CREACION")));
				o.setFEC_CREACION(c.getString(c.getColumnIndex("FEC_CREACION")));
				o.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
				o.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
				lista.add(o);
			} while (c.moveToNext());
		} else {
			lista = null;
		}
		cerrar();
		return lista;
	}

	@Override
	public Cap300 buscar(Cap300 o) {
		db = util.getReadableDatabase();
		c = db.rawQuery(
				"select * from CAP300 where ID_N=? and ANIO=? and NRO_MREG=? and NRO_VFREG=?",
				new String[] { o.getID_N(), o.getANIO() + "",
						o.getNRO_MREG() + "", o.getNRO_VFREG() + "" });
		if (c.moveToFirst()) {
			o.setIVH301A(c.getString(c.getColumnIndex("IVH301A")));
			o.setIVH301B(c.getString(c.getColumnIndex("IVH301B")));
			o.setIVH301C(c.getString(c.getColumnIndex("IVH301C")));
			o.setIVH302(c.getInt(c.getColumnIndex("IVH302")));
			o.setIVH302_N(c.getString(c.getColumnIndex("IVH302_N")));
			o.setIVH303(c.getInt(c.getColumnIndex("IVH303")));
			o.setIVH304_DIA(c.getString(c.getColumnIndex("IVH304_DIA")));
			o.setIVH304_MES(c.getString(c.getColumnIndex("IVH304_MES")));
			o.setIVH304_ANIO(c.getString(c.getColumnIndex("IVH304_ANIO")));
			o.setIVH305(c.getInt(c.getColumnIndex("IVH305")));
			o.setIVH306(c.getInt(c.getColumnIndex("IVH306")));
			o.setIVH306_O(c.getString(c.getColumnIndex("IVH306_O")));
			o.setIVH307(c.getInt(c.getColumnIndex("IVH307")));
			o.setIVH307_O(c.getString(c.getColumnIndex("IVH307_O")));
			o.setIVH308(c.getInt(c.getColumnIndex("IVH308")));
			o.setIVH309(c.getInt(c.getColumnIndex("IVH309")));
			o.setIVH309_O(c.getString(c.getColumnIndex("IVH309_O")));
			o.setOBS_3(c.getString(c.getColumnIndex("OBS_3")));
			o.setFEC_INI(c.getString(c.getColumnIndex("FEC_INI")));
			o.setHOR_INI(c.getString(c.getColumnIndex("HOR_INI")));
			o.setFEC_FIN(c.getString(c.getColumnIndex("FEC_FIN")));
			o.setHOR_FIN(c.getString(c.getColumnIndex("HOR_FIN")));
			o.setUSR_CREACION(c.getString(c.getColumnIndex("USR_CREACION")));
			o.setFEC_CREACION(c.getString(c.getColumnIndex("FEC_CREACION")));
			o.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
			o.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
		} else {
			o = null;
		}
		cerrar();
		return o;
	}

	@Override
	public int crear(Cap300 o) {
		db = util.getWritableDatabase();
		db.beginTransaction();
		cv.put("ID_N", o.getID_N());
		cv.put("ANIO", o.getANIO());
		cv.put("NRO_MREG", o.getNRO_MREG());
		cv.put("NRO_VFREG", o.getNRO_VFREG());
		cv.put("IVH301A", o.getIVH301A());
		cv.put("IVH301B", o.getIVH301B());
		cv.put("IVH301C", o.getIVH301C());
		cv.put("IVH302", o.getIVH302());
		cv.put("IVH302_N", o.getIVH302_N());
		cv.put("IVH303", o.getIVH303());
		cv.put("IVH304_DIA", o.getIVH304_DIA());
		cv.put("IVH304_MES", o.getIVH304_MES());
		cv.put("IVH304_ANIO", o.getIVH304_ANIO());
		cv.put("IVH305", o.getIVH305());
		cv.put("IVH306", o.getIVH306());
		cv.put("IVH306_O", o.getIVH306_O());
		cv.put("IVH307", o.getIVH307());
		cv.put("IVH307_O", o.getIVH307_O());
		cv.put("IVH308", o.getIVH308());
		cv.put("IVH309", o.getIVH309());
		cv.put("IVH309_O", o.getIVH309_O());
		cv.put("OBS_3", o.getOBS_3());
		cv.put("FEC_INI", o.getFEC_INI());
		cv.put("HOR_INI", o.getHOR_INI());
		cv.put("FEC_FIN", o.getFEC_FIN());
		cv.put("HOR_FIN", o.getHOR_FIN());

		String date = "";
		while (date.equals("")) {
			date = obtenerDate();
		}
		cv.put("USR_CREACION", o.getUSR_CREACION());
		cv.put("FEC_CREACION", date);
		cv.put("USR_EDICION", o.getUSR_EDICION());
		cv.put("FEC_EDICION", date);

		int res = (int) db.insert("CAP300", null, cv);
		if (res != -1) {
			db.setTransactionSuccessful();
		}
		cerrar();
		return res;
	}

	@Override
	public int actualizar(Cap300 o) {
		db = util.getWritableDatabase();
		db.beginTransaction();
		cv.put("IVH301A", o.getIVH301A());
		cv.put("IVH301B", o.getIVH301B());
		cv.put("IVH301C", o.getIVH301C());
		cv.put("IVH302", o.getIVH302());
		cv.put("IVH302_N", o.getIVH302_N());
		cv.put("IVH303", o.getIVH303());
		cv.put("IVH304_DIA", o.getIVH304_DIA());
		cv.put("IVH304_MES", o.getIVH304_MES());
		cv.put("IVH304_ANIO", o.getIVH304_ANIO());
		cv.put("IVH305", o.getIVH305());
		cv.put("IVH306", o.getIVH306());
		cv.put("IVH306_O", o.getIVH306_O());
		cv.put("IVH307", o.getIVH307());
		cv.put("IVH307_O", o.getIVH307_O());
		cv.put("IVH308", o.getIVH308());
		cv.put("IVH309", o.getIVH309());
		cv.put("IVH309_O", o.getIVH309_O());
		cv.put("OBS_3", o.getOBS_3());
		cv.put("FEC_INI", o.getFEC_INI());
		cv.put("HOR_INI", o.getHOR_INI());
		cv.put("FEC_FIN", o.getFEC_FIN());
		cv.put("HOR_FIN", o.getHOR_FIN());

		String date = "";
		while (date.equals("")) {
			date = obtenerDate();
		}
		if (o.getFEC_CREACION() == null) {
			cv.put("FEC_CREACION", date);
		} else if (o.getFEC_CREACION().equals("")) {
			cv.put("FEC_CREACION", date);
		}
		cv.put("USR_EDICION", o.getUSR_EDICION());
		cv.put("FEC_EDICION", date);

		int res = (int) db.update("CAP300", cv,
				"ID_N=? and ANIO=? and NRO_MREG=? and NRO_VFREG=?",
				new String[] { o.getID_N(), o.getANIO() + "",
						o.getNRO_MREG() + "", o.getNRO_VFREG() + "" });
		if (res > 0) {
			db.setTransactionSuccessful();
		}
		cerrar();
		return res;
	}

	@Override
	public int eliminar(Cap300 o) {
		try {
			db = util.getReadableDatabase();
			Cap200 c200 = new Cap200();
			c = db.rawQuery(
					"Select IH212 from CAP200 where ID_N=? AND ANIO=? AND NRO_MREG=?",
					new String[] { o.getID_N(), o.getANIO() + "",
							o.getNRO_MREG() + "" });
			if (c.moveToFirst()) {
				c200.setID_N(o.getID_N());
				c200.setANIO(o.getANIO());
				c200.setNRO_MREG(o.getNRO_MREG());

				c200.setIH212(c.getInt(0));
			}
			db.close();
			c.close();
			db = util.getWritableDatabase();
			//db.beginTransaction();
			int res = db.delete(
					"CAP300",
					"ID_N=? AND ANIO=? AND NRO_MREG=? AND NRO_VFREG=?",
					new String[] { o.getID_N(), o.getANIO() + "",
							o.getNRO_MREG() + "", o.getNRO_VFREG() + "" });
			if (res != 0) {

				cv = new ContentValues();
				cv.put("IH212", c200.getIH212() - 1);
				res = db.update(
						"CAP200",
						cv,
						"ID_N=? AND ANIO=? AND NRO_MREG=?",
						new String[] { o.getID_N(), o.getANIO() + "",
								o.getNRO_MREG() + "" });
				if (res == 0) {
					throw new Exception("no pudo actualizar el cap 200");
				}

				db.close();
				List<Cap300> l300 = leer(o.getID_N(), o.getANIO(),
						o.getNRO_MREG());
				db.close();
				db = util.getWritableDatabase();
				if (l300 != null) {
					for (int i = 1; i <= l300.size(); i++) {
						Cap300 c300 = l300.get(i - 1);
						cv = new ContentValues();
						cv.put("NRO_VFREG", i);
						res = db.update(
								"CAP300",
								cv,
								"ID_N=? AND ANIO=? AND NRO_MREG=? AND NRO_VFREG=?",
								new String[] { c300.getID_N(),
										c300.getANIO() + "",
										c300.getNRO_MREG() + "",
										c300.getNRO_VFREG() + "" });
						if (res == 0) {
							throw new Exception("resultado == 0 lista 300");
						}
					}
				}
			} else {
				throw new Exception("resultado == 0 lista 300");
			}

			//db.setTransactionSuccessful();

			cerrar();

			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("DAO", e.getMessage());

		}
		cerrar();
		return 0;
	}

	public String obtenerDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(Calendar.getInstance().getTime());
	}
}
